package com.lgy.web.security;

import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * Created by fengch on 2017/5/29.
 */
public class MyAuthenticationProvider  implements AuthenticationProvider {
    private UserDetailsService userDetailsService;

    public void setUserDetailsService(UserDetailsService userDetailsService) {
        this.userDetailsService = userDetailsService;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        //username
        System.out.println("user name: "+authentication.getName());
        //password
        System.out.println("password: "+authentication.getCredentials());

        System.out.println("getPrincipal: "+authentication.getPrincipal());
        System.out.println("getAuthorities: "+authentication.getAuthorities());
        System.out.println("getDetails: "+authentication.getDetails());

        UserDetails userDetails = this.userDetailsService.loadUserByUsername(authentication.getName());

        //对用户名的密码进行校验
        UsernamePasswordAuthenticationToken result =  new UsernamePasswordAuthenticationToken(
                                                        userDetails,
                                                        authentication.getCredentials(),
                                                        userDetails.getAuthorities()) ;
        return result;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
